package cn.fl.risknew.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.fl.project.service.IPrjBscInfoService;
import cn.fl.ref.fribaseinfo.vo.FriBaseInfoVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.mid.core.framework.page.Pager;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.customer.service.ICstBscInfoService;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.overdue.condition.OverdueBaseCondition;
import cn.fl.risknew.service.IRiskOverdueService;
import cn.fl.risknew.vo.OverdueCstVO;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

/**
 * @Description: 租金提醒用户与组织架构关系表 控制层
 */
@Controller
@RequestMapping("/risknew/rskUsrOrgR")
public class RskUsrOrgRNewController extends BaseController {
	private static final Logger logger = LoggerFactory.getLogger(RskUsrOrgRNewController.class);
	/**
     * 客户 服务层BEAN
     */
    @Autowired
    private ICstBscInfoService cstBscInfoService;
    @Autowired
    private IRiskOverdueService overdueService;
    @Autowired
    private IPrjBscInfoService prjBscInfoService;
	/**
     * 到列表页面
     */
    @RequestMapping(value = "/toConfigPage.do", method = {RequestMethod.GET})
    public ModelAndView toConfigPage(HttpServletRequest request, HttpServletResponse response) {
        BaseLogUtils.info(logger, "toConfigPage", "加载任务分配列表页面.", CurrentThreadContext.getCurrentUserName());
        ModelAndView mav = new ModelAndView("risknew/rskUsrOrgR/taskConfig");
        return mav;
    }
    
    /**
     * 到逾期任务列表页
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/toOvdListPage.do", method = {RequestMethod.GET})
    public ModelAndView toOvdListPage(HttpServletRequest request, HttpServletResponse response) {
        BaseLogUtils.info(logger, "toOvdListPage", "加载到逾期任务列表页面.", CurrentThreadContext.getCurrentUserName());
        ModelAndView mav = new ModelAndView();
        mav.setViewName("risknew/rskUsrOrgR/ovdCollectTaskConfig");
        // 事业部
        List<KeyValueVO> orgList = this.cstBscInfoService.searchBusinessOrgList();
        mav.addObject("buOrgCdList", JSON.toJSONString(orgList));

        //查询第三方机构
        List<FriBaseInfoVO> list = this.overdueService.getFriBaseInfoList();
        mav.addObject("friBaseList", list);

        //查询项目类型
        List<KeyValueVO> projectTypeList = this.prjBscInfoService.getPrjTypCdALLKV();
        mav.addObject("projectTypeList", projectTypeList);


        return mav;
    }
    /***********************
     * 初始化逾期催收列表
     ***********************/
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/initOvdList.do", method = {RequestMethod.GET})
    @ResponseBody
    public Object initOvdList(HttpServletRequest request, HttpServletResponse response) {
        String currentPage = request.getParameter("page");
        String pageSize = request.getParameter("pagesize");
        Pager page = new Pager();
        if (StringUtils.isNotEmpty(currentPage)) {
            page.setCurrentPage(Integer.parseInt(currentPage));
        }

        if (StringUtils.isNotEmpty(pageSize)) {
            page.setEveryPage(Integer.parseInt(pageSize));
        }

        OverdueBaseCondition qc = new OverdueBaseCondition();
        String queryType = request.getParameter("queryType");//类型：1.逾期任务 2.组任务 3.我的任务
        String cstNm = request.getParameter("cstNm");
        String lsCntNo = request.getParameter("lsCntNo");
        String buOrgCds = request.getParameter("buOrgCd");
        String ovdDtMin = request.getParameter("ovdDtMin");
        String ovdDtMax = request.getParameter("ovdDtMax");
        String ovdDaysMin = request.getParameter("ovdDaysMin");
        String ovdDaysMax = request.getParameter("ovdDaysMax");
        String ovdAmtMin = request.getParameter("ovdAmtMin");
        String ovdAmtMax = request.getParameter("ovdAmtMax");
        String allocationFlag=request.getParameter("allocationFlag");
        String usrNm = request.getParameter("usrNm");
        String cstMgrNm = request.getParameter("cstMgrNm");
        String prjTypCd = request.getParameter("prjTypCd");
        String insCode = request.getParameter("insCode");
        String tableNm=request.getParameter("tableNm");
        String maxPd=request.getParameter("maxPd");


        List<String> buOrgCdList = new ArrayList<>();
        if(StringUtils.isNotBlank(buOrgCds)){
            String[] buOrgCdStr = buOrgCds.split(";");
            if(buOrgCdStr.length>0){
                for (String str : buOrgCdStr) {
                    buOrgCdList.add(str);
                }
            }
        }

        if (StringUtils.isNotEmpty(queryType)) {
            qc.setQueryType(queryType);
        }
        if (StringUtils.isNotEmpty(cstNm)) {
            qc.setCstNm(cstNm);
        }
        if (StringUtils.isNotBlank(lsCntNo)) {
            qc.setLsCntNo(lsCntNo.trim());
        }
        if (CollectionUtils.isNotEmpty(buOrgCdList)) {
            qc.setBuOrgCdList(buOrgCdList);
        }
        if (StringUtils.isNotBlank(ovdDtMin)) {
            qc.setOvdDtMin(ovdDtMin);
        }
        if (StringUtils.isNotBlank(ovdDtMax)) {
            qc.setOvdDtMax(ovdDtMax);
        }
        if (StringUtils.isNotBlank(ovdDaysMin)) {
            qc.setOvdDaysMin(Integer.valueOf(ovdDaysMin));
        }
        if (StringUtils.isNotBlank(ovdDaysMax)) {
            qc.setOvdDaysMax(Integer.valueOf(ovdDaysMax));
        }
        if (StringUtils.isNotBlank(ovdAmtMax) && StringUtils.isUnsignedNumeric(ovdAmtMax)) {
            Double amtMax = 100 * Double.parseDouble(ovdAmtMax);
            qc.setOvdAmtMax(amtMax.longValue());
        }
        if (StringUtils.isNotBlank(ovdAmtMin) && StringUtils.isUnsignedNumeric(ovdAmtMin)) {
            Double amtMin = 100 * Double.parseDouble(ovdAmtMin);
            qc.setOvdAmtMin(amtMin.longValue());
        }
        if(StringUtils.isNotBlank(allocationFlag)){
            qc.setAllocationFlag(Integer.parseInt(allocationFlag));
        }
        if(StringUtils.isNotBlank(usrNm)){
            qc.setUsrNm(usrNm);
        }
        if(StringUtils.isNotBlank(cstMgrNm)){
            qc.setCstMgrNm(cstMgrNm);
        }
        if (StringUtils.isNotEmpty(prjTypCd)) {
        	qc.setPrjTypCd(prjTypCd);;
        }
        if(StringUtils.isNotBlank(insCode)){
			qc.setInsCode(insCode);
		}
        /*if (StringUtils.isNotEmpty(fundRasInsId)) {
        	qc.setFundRasInsId(Long.valueOf(fundRasInsId));
        }*/

        if(StringUtils.isNotBlank(tableNm)){
            qc.setTableNm(tableNm);
        }
        if(StringUtils.isNotBlank(maxPd)){
            qc.setMaxPd(Integer.parseInt(maxPd));
        }
        qc.setUsrId(CurrentThreadContext.getCurrentUserId());
        qc.setSysCd(CurrentThreadContext.getCurrentSysCd());
        page.setCondition(qc);

        BaseLogUtils.info(logger, "initOvdList", "初始化逾期催收列表数据.DATA:" + JSONObject.toJSONString(page), CurrentThreadContext.getCurrentUserName());
        Pager pager = this.overdueService.getTaskOverdueCustomerListPage(qc, page.getCurrentPage(), page.getEveryPage());
        List<OverdueCstVO> list = (List<OverdueCstVO>) pager.getResultList();
        ListPageVO<OverdueCstVO> listPage = new ListPageVO<OverdueCstVO>(list, pager.getTotalCount());
        return listPage;
    }
}
